package com.dokimall.product.service.impl;

import com.dokimall.product.dao.AttrAttrgroupRelationDao;
import com.dokimall.product.entity.AttrAttrgroupRelationEntity;
import com.dokimall.product.entity.AttrEntity;
import com.dokimall.product.service.AttrAttrgroupRelationService;
import com.dokimall.product.service.AttrService;
import com.dokimall.product.vo.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dokimall.common.utils.PageUtils;
import com.dokimall.common.utils.Query;

import com.dokimall.product.dao.AttrGroupDao;
import com.dokimall.product.entity.AttrGroupEntity;
import com.dokimall.product.service.AttrGroupService;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    AttrAttrgroupRelationService attrAttrgroupRelationService;

    @Autowired
    AttrService attrService;

    @Autowired
    AttrAttrgroupRelationDao attrAttrgroupRelationDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        String key = (String)params.get("key");
        QueryWrapper<AttrGroupEntity> queryWrapper = new QueryWrapper<AttrGroupEntity>();
        if(StringUtils.isNotEmpty(key)){
            queryWrapper.and((obj)->{
                obj.eq("attr_group_id",key).or().like("attr_group_name",key);
            });
        }
        if(catelogId==0){
            IPage<AttrGroupEntity> page = this.page(
            new Query<AttrGroupEntity>().getPage(params),queryWrapper);
            return new PageUtils(page);
        }else {
            queryWrapper.eq("catelog_id", catelogId);
            IPage<AttrGroupEntity> page = this.page(
                    new Query<AttrGroupEntity>().getPage(params),queryWrapper);
            return new PageUtils(page);
        }


    }

    @Override
    public List<AttrEntity> getAttrRelation(Long attrgroupId) {
        List<AttrAttrgroupRelationEntity> relationEntities = attrAttrgroupRelationService.list(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_group_id", attrgroupId));
        List<Long> attrIds = relationEntities.stream().map((attr) -> {
            return attr.getAttrId();
        }).collect(Collectors.toList());
        if(attrIds ==null || attrIds.size() <= 0){
            return null;
        }
        Collection<AttrEntity> attrEntities = attrService.listByIds(attrIds);
        return (List<AttrEntity>) attrEntities;


    }

    @Override
    public void attrRelationDelete(AttrRelDelVo[] attrRelDelVo) {

        List<AttrAttrgroupRelationEntity> relationEntities = Arrays.asList(attrRelDelVo).stream().map((attr) -> {
            AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
            BeanUtils.copyProperties(attr, relationEntity);
            return relationEntity;
        }).collect(Collectors.toList());
        attrAttrgroupRelationDao.deleteByIds(relationEntities);
    }

    @Override
    public void saveBatch(List<AttrRelDelVo> vos) {
        List<AttrAttrgroupRelationEntity> relationEntities = vos.stream().map((item) -> {
            AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
            BeanUtils.copyProperties(item, relationEntity);
            return relationEntity;
        }).collect(Collectors.toList());
        attrAttrgroupRelationService.saveBatch(relationEntities);
    }

    @Override
    public List<AttrGroupVo> getAttrByCatelogId(Long catelogId) {
        List<AttrGroupEntity> groupEntities = this.list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));
        List<AttrGroupVo> vos = groupEntities.stream().map((item) -> {
            AttrGroupVo attrGroupVo = new AttrGroupVo();
            BeanUtils.copyProperties(item, attrGroupVo);
            List<AttrAttrgroupRelationEntity> attrgroupRelationEntities = attrAttrgroupRelationService.list(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_group_id", item.getAttrGroupId()));
            List<AttrEntity> attrEntityList = attrgroupRelationEntities.stream().map((relation) -> {
                AttrEntity attrEntity = attrService.getById(relation.getAttrId());
                return attrEntity;
            }).collect(Collectors.toList());
            attrGroupVo.setAttrs(attrEntityList);
            return attrGroupVo;
        }).collect(Collectors.toList());
        return vos;
    }

    @Override
    public List<SpuItemAttrGroupVo> getSpuAttrInfo(Long spuId, Long catalogId) {
        return this.baseMapper.getSpuAttrInfo(spuId,catalogId);
    }


}